package com.spring.security.core.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.util.JSONPObject;

@Aspect
@Component
public class SpringJsonpSupport {
	@Pointcut("@annotation(com.spring.security.core.annotation.JsonpSupport)")
	public void jsonpSupport(){
	}
	
	@Around("jsonpSupport()")
	public Object aroundExec(ProceedingJoinPoint pjp) throws Throwable{
		Object[] parameters=pjp.getArgs();
		if(parameters[parameters.length-1] instanceof String) {
			if(parameters[parameters.length-1]!=null){
				String callback=(String)parameters[parameters.length-1];
				return new JSONPObject(callback,pjp.proceed());
			}
		}
		return pjp.proceed();
	}
}
